package com.it2297.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import com.it2297.controller.CompanyController;
import com.it2297.main.CompanyApp;

import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.MatteBorder;
import javax.swing.UIManager;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class CompanyLogin extends JFrame {
	
	
	private JMenuBar menuBar;
	private JMenu menuHome;
	private JMenu menuExit;
	private JMenu menuJobAd;
	private JMenuItem menuRemoveJob;
	private JMenuItem menuCreateJob;
	private JMenuItem menuViewAllJobsByCompany;
	private JMenu menuApplicants;
	private JMenuItem menuViewApplicants;
	private JSeparator separator;
	private Component horizontalGlue;
	private Component horizontalStrut;
	private JLabel lblWelcome;
	private JMenuItem menuLogout;
	private JMenuItem menuhomescreen;
	private JMenu mnProfile;
    private JMenuItem mntmUpdateCompany;
	
	
	
	
	
	private JPanel contentPane;
	private JTextField tf_name;
	private JPasswordField pwd;
	private String companyName;
	private String password;
	final CompanyController cct= new CompanyController(this);
	private String valid;
	private JLabel lblNewLabel_3;
	private JLabel lblimg;
	private JPanel panel;
	private JPanel panel_1;
	private JSeparator separator_3;
	private JSeparator separator_4;
	private JLabel lblForgotPasswordClick;
	private JMenu mnBackToMain;
	private JMenuItem mntmBackToMain;
		
		public String getCompanyName(){
			return tf_name.getText().toString();
		}
		
		public String getPassword(){
			String password="";
			for(int i=0;i<pwd.getPassword().length;i++){
				password +=pwd.getPassword()[i];
			}
			return password;
		}
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CompanyLogin frame = new CompanyLogin();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	
		
		/**
		 * Create the panel.
		 */
		public CompanyLogin() {
			
			String currentWorkingDirectory = System.getProperty("user.dir");
			setTitle("Company - Company Login");
			setBounds(100, 100, 740, 560);
			//contentPane = new JPanel();
			//contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
			//setContentPane(contentPane);
		//	contentPane.setLayout(null);
			setLocationRelativeTo(null);
			getContentPane().setLayout(null);
			
			tf_name = new JTextField();
			tf_name.setBounds(287, 53, 164, 28);
			getContentPane().add(tf_name);
			tf_name.setColumns(10);
			
			pwd = new JPasswordField();
			pwd.setBounds(461, 53, 164, 28);
			getContentPane().add(pwd);
			
			JLabel lblNewLabel = new JLabel("Company Name:");
			lblNewLabel.setForeground(new Color(128, 0, 0));
			lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 15));
			lblNewLabel.setBounds(287, 35, 132, 21);
			getContentPane().add(lblNewLabel);
			
			JLabel lblNewLabel_1 = new JLabel("Password:");
			lblNewLabel_1.setForeground(new Color(128, 0, 0));
			lblNewLabel_1.setFont(new Font("Times New Roman", Font.BOLD, 15));
			lblNewLabel_1.setBounds(461, 38, 83, 14);
			getContentPane().add(lblNewLabel_1);
			
			JLabel lblNewLabel_2 = new JLabel();
			lblNewLabel_2.setForeground(new Color(128, 0, 0));
	//		lblNewLabel_2.setIcon(new ImageIcon(currentWorkingDirectory + "/img/Job/arrow2.png"));
			lblNewLabel_2.setIcon(new ImageIcon(currentWorkingDirectory + "/img/Job/logo.jpg"));
			lblNewLabel_2.setBounds(10, 31, 132, 73);
			lblNewLabel_2.setFont(new Font("Stencil Std", Font.BOLD, 25));
			getContentPane().add(lblNewLabel_2);	
			
			/*JMenuBar menuBar_1 = new JMenuBar();
			menuBar_1.setBounds(0, 0, 724, 24);
			getContentPane().add(menuBar_1);
			*/
			
			JButton btnLogin = new JButton("Login");
			btnLogin.setBackground(UIManager.getColor("Button.darkShadow"));
			btnLogin.setForeground(new Color(128, 0, 0));
			btnLogin.setBounds(635, 53, 79, 27);
			getContentPane().add(btnLogin);
			btnLogin.setFont(new Font("Times New Roman", Font.BOLD, 15));
			btnLogin.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					if (tf_name.getText().isEmpty() || new String(pwd.getPassword()).isEmpty())
					{
						JOptionPane.showMessageDialog(null, "Please fill in all the necessary details.");
					}
					else
					{
			
								cct.validateLogin();
						
								
								if (CompanyApp.isUserLogin())
								{
									CompanyHomePage chp=new CompanyHomePage();
									chp.setLocationRelativeTo(null);
									chp.setVisible(true);
									chp.setTimer("start");
									chp.setTitle("Company - Home Screen");
									closeWindow();
									
	
								}
					}	
				}
			});
			
			JLabel lbl = new JLabel("New label");
			lbl.setForeground(new Color(255, 255, 255));
			lbl.setFont(new Font("Tahoma", Font.PLAIN, 14));
			lbl.setText("<html>First time visiting?<br/><b>Place your First Job Ad Right Now!</b><br/><br/><ul><li>Reach a large audience of job seekers.</li><br/><li>Publish your job ads any time.</li><br/><li>Get quick responses from relevant job seekers.</li></ul><br/><br/><b>What are you waiting for? Register your company today!</b></html>");
			lbl.setBounds(402, 104, 294, 418);
			getContentPane().add(lbl);
			
			lblForgotPasswordClick = new JLabel("<html><u>Forgot Password? Click here!</u></html>");
			lblForgotPasswordClick.addMouseListener(new MouseAdapter() {
				@Override
				public void mousePressed(MouseEvent arg0) {
					CompanyRecoverPassword crp= new CompanyRecoverPassword();
					crp.setVisible(true);
				}
			});			
			lblForgotPasswordClick.setForeground(Color.BLUE);
			lblForgotPasswordClick.setBounds(461, 83, 253, 21);
			getContentPane().add(lblForgotPasswordClick);
			
			JLabel lblClickHereTo = new JLabel("<html><u>Click here to register!</u></html>");
			lblClickHereTo.setForeground(Color.BLUE);
			lblClickHereTo.addMouseListener(new MouseAdapter() {
				@Override
				public void mousePressed(MouseEvent arg0) {
					CompanyCreateCompany ccc= new CompanyCreateCompany();
					ccc.setVisible(true);
				}
			});
			lblClickHereTo.setBounds(287, 83, 175, 21);
			getContentPane().add(lblClickHereTo);
			
			
			
			
			
			

			menuBar = new JMenuBar();
			menuBar.setBounds(0, 0, 724, 24);
			getContentPane().add(menuBar);
		
			menuHome = new JMenu("Home");
			menuBar.add(menuHome);
			menuhomescreen=new JMenuItem("Home Screen");
			menuhomescreen.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				}
			});
			menuHome.add(menuhomescreen);
			
			menuJobAd=new JMenu("Job Advertisement");
			menuBar.add(menuJobAd);
			menuCreateJob=new JMenuItem("Create Job Advertisement");
			menuCreateJob.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JobCreateJob jcj=new JobCreateJob();
					jcj.setLocationRelativeTo(null);
					jcj.setVisible(true);
					closeWindow();
				}
			});
			menuJobAd.add(menuCreateJob);
			
			JSeparator separator_1 = new JSeparator();
			menuJobAd.add(separator_1);
			menuRemoveJob=new JMenuItem("Remove Job Advertisement");
			menuJobAd.add(menuRemoveJob);
			
			JSeparator separator_2 = new JSeparator();
			menuJobAd.add(separator_2);
			menuViewAllJobsByCompany=new JMenuItem("View All Jobs By Company");
			menuViewAllJobsByCompany.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					CompanyViewAllJobsByCompany cvajbc=new CompanyViewAllJobsByCompany();
					cvajbc.setLocationRelativeTo(null);
					cvajbc.setVisible(true);
					cvajbc.setTitle("Company - View All Jobs By Company ");
					closeWindow();
				}
			});
			menuJobAd.add(menuViewAllJobsByCompany);
			
	/*		menuApplicants=new JMenu("Applicants");
			menuBar.add(menuApplicants);
			menuViewApplicants=new JMenuItem("View Applicants");
			menuViewApplicants.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					JobViewApplicantsTest cvajbc = new JobViewApplicantsTest();
					cvajbc.setLocationRelativeTo(null);
					cvajbc.setVisible(true);
					cvajbc.setTitle("Company - View Job Applicant ");
					closeWindow();
				}
			});
			menuApplicants.add(menuViewApplicants);
	*/		
			horizontalGlue = Box.createHorizontalGlue();
			menuBar.add(horizontalGlue);
			
			
			
			lblWelcome = new JLabel();
			lblWelcome.setForeground(new Color(0, 0, 128));
			if (CompanyApp.isUserLogin())
			{
				lblWelcome.setText("Welcome, " + CompanyApp.getCurrentUser().getCompanyName() + ".");
			}
			else
			{
				lblWelcome.setText("Welcome! Please login to manage your job advertisements.");
			}
			menuBar.add(lblWelcome);
			
			horizontalStrut = Box.createHorizontalStrut(20);
			menuBar.add(horizontalStrut);
			
			
			mnProfile = new JMenu("Profile");
			menuBar.add(mnProfile);
			
			mntmUpdateCompany = new JMenuItem("Update Company");
			mntmUpdateCompany.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					CompanyUpdateCompany cuc = new CompanyUpdateCompany();
					cuc.setVisible(true);
					cuc.setTitle("Update Company Details");
					closeWindow();
				}
			});
			mnProfile.add(mntmUpdateCompany);
			
			mnBackToMain = new JMenu("Main Login Page");
			menuBar.add(mnBackToMain);
			
			mntmBackToMain = new JMenuItem("Back to main login page");
			mntmBackToMain.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					//JOptionPane.showMessageDialog(null, "NOT DONE YET!!!!!!!!!!");
					LoginView clv = new LoginView();
					clv.setVisible(true);
					clv.setLocationRelativeTo(null);
					closeWindow();
				}
			});
			mnBackToMain.add(mntmBackToMain);
			
			
			menuExit = new JMenu("Exit");
			menuBar.add(menuExit);
			menuLogout=new JMenuItem("Logout");
			menuLogout.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					CompanyApp.logoutUser();
					
					CompanyLogin loginUI = new CompanyLogin();
					loginUI.setLocationRelativeTo(null);
					loginUI.setVisible(true);
					closeWindow();
				}
			});
			menuExit.add(menuLogout);
			
			

			
			lblimg = new JLabel("");
			lblimg.setHorizontalAlignment(SwingConstants.CENTER);
			lblimg.setBounds(10, 104, 373, 418);
			lblimg.setIcon(new ImageIcon(currentWorkingDirectory + "/img/Job/job3.jpg"));
			getContentPane().add(lblimg);
			
		/*	separator_3 = new JSeparator();
			separator_3.setBackground(new Color(255, 255, 255));
			separator_3.setForeground(new Color(0, 0, 0));
			separator_3.setBounds(0, 126, 724, 2);
			getContentPane().add(separator_3);*/
	
			JPanel panel = new JPanel();
			panel.setBackground(Color.WHITE);
		//	panel.setBackground(new Color(230, 230, 250));
			panel.setBounds(0, 21, 724, 83);
			getContentPane().add(panel);
		
			panel_1 = new JPanel();
			panel_1.setBackground(new Color(128, 0, 0));
			panel_1.setBounds(0, 104, 724, 418);
			getContentPane().add(panel_1);
			
		
			
			
		
			
			
			
			if (CompanyApp.isUserLogin())
			{
				 menuHome.setEnabled(true);
				 menuExit.setEnabled(true);
				 menuJobAd.setEnabled(true);
				 mnProfile.setEnabled(true);
				// menuApplicants.setEnabled(true);
			}
			else
			{
				 menuHome.setEnabled(false);
				 menuExit.setEnabled(false);
				 menuJobAd.setEnabled(false);
				 mnProfile.setEnabled(false);
				// menuApplicants.setEnabled(false);
			}
			
			
			
			
		}
	
		
		public void closeWindow()
		{
			this.dispose();
		}
}
